﻿using System;

public class Person
{
	private static String[] nameList = {"Andrew", "Nathan", "Paul", "Andrea", "Sam", "Frank", "Martha", "Eric",
        "Julie", "Craig", "Stuart", "Kim", "John", "Randy", "Kuda"};
    private static Random rand = new Random();
    private String name;

    private Elevator elevator;
    
    private boolean inElevator;
    private int currentFloor;
    private int futureFloor;
    
    private Building building;
    private Date timer;
    
    public Person(Building b, int cF, int fF){
        this.name = nameList[java.lang.Math.abs(rand.nextInt()%(nameList.length))];
        this.currentFloor = cF;
        this.futureFloor = fF;
        this.timer = new Date();
        this.building = b;
        this.elevator = null;
        this.inElevator = false;
        this.building.addPersonToFloor(this.currentFloor);
        System.out.println(this.name + " on floor " + this.currentFloor);
    }

    public Person(Building b) {
        this.name = nameList[java.lang.Math.abs(rand.nextInt()%(nameList.length))];
        this.currentFloor = rand.nextInt()%(b.getNumberOfFLoors());
        do {
            this.futureFloor = rand.nextInt()%(b.getNumberOfFLoors());
        } while (this.futureFloor == this.currentFloor);
        this.building = b;
        this.elevator = null;
        this.inElevator = false;
        this.building.addPersonToFloor(this.currentFloor);
        System.out.println(this.name + " on floor " + this.currentFloor);
    }
    /*
     * Sets the floor the person wishes to go to next
     */
    void setFutureFloor(int f){
        this.futureFloor = f;
    }
    
        
    /*
     * Gets the floor the person wishes to go to next
     */
    int getFutureFloor(){
        return this.futureFloor;
    }
    
    /*
     * Changes the current floor for the person
     * Used for when they get off the elevator
     */
    void changeFloor(int f){
        this.currentFloor = f;
        System.out.println(name + " is now on floor " + f);
    }

    /*
     * Returns the floor the person is currently on
     */
    int getCurrentFloor(){
        return this.currentFloor;
    }
    
    /*
     * Allows the person to call the elevator after a specified time;
     */
    void callElevator(){
        if (this.currentFloor != this.futureFloor){
            if ((new Date().getTime() - this.timer.getTime()) > 5000 && !this.inElevator()){
                if (this.currentFloor < this.futureFloor){
                    building.requestElevator(true, currentFloor);
                } else {
                    building.requestElevator(false, currentFloor);
                }
                this.timer = new Date();
            }
        } // else assign different futurefloor
    }
    
    /*
     * Returns the elevator the person is on, if they are not on an elevator
     * returns a null object.
     */
    boolean inElevator(){
        return this.inElevator;
    }
    
    void setInElevator(boolean inside){
        System.out.println(name + " is " + inside + " in now in elevator");
        this.inElevator = inside;
    }
    
    void setElevator(Elevator e){
        this.elevator = e;
    }
    
    Elevator getElevator(){
        return this.elevator;
    }
    
    void resetTimer(){
        
    }
}
